/*
 * Created on 2008-06-04
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package elka.isi.operator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import elka.isi.individual.AnimalIndividual;
import elka.isi.individual.HerbivoreAnimalIndivdual;
import elka.isi.individual.RabbitIndividual;
import elka.isi.util.RWUtils;
import org.evolvica.core.IIndividual;
import org.evolvica.core.IIndividualSet;
import org.evolvica.core.IndividualList;
import org.evolvica.core.population.SortedPopulation;
import org.evolvica.util.RandomUtils;

import elka.isi.individual.HerbivoreAnimalIndivdual;
import elka.isi.individual.WolfIndividual;
import elka.isi.util.RWUtils;

/**
 * @author Zibi
 * 
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class HuntingEcosystem extends SortedEcosystem {

	/*
	 * (non-Javadoc)
	 * 
	 * @see elka.isi.SortedEcosystem#insert(org.evolvica.core.IIndividualSet)
	 */

	public IIndividualSet insert(IIndividualSet iset) {
		// RWUtils.getActualStats(iset);

		List huntingWolves = Arrays.asList(RWUtils.getWolves(iset).toArray());
		Collections.sort(huntingWolves, comparator);
		//System.out.println("=================================");
		for (Iterator iterator = huntingWolves.iterator();
			iterator.hasNext();
			) {
			AnimalIndividual individualWolf =
				(AnimalIndividual) iterator.next();
			//IIndividual[] wolves = RWUtils.getWolves(iset).toArray();
			int oldWolfApetite = wolfApetite;
			int nowwwy = (int)((int)individualWolf.getAppetiteScore() / 30.0 );
			int nowy = ( nowwwy- 5 );
			wolfApetite = wolfApetite + nowy;
			
	//		System.out.println(wolfApetite +" <= ");
			IIndividualSet weakerHerbivoes =
				RWUtils.getWeaker(
					RWUtils.getHerbivores(iset).toArray(),
					individualWolf,
					comparator);

			//todo: prosba o komentarz jaka byla idea / Jacek
			//na razie uzywam jednego catchRatio dla krolikow i saren
			IIndividual individual;
			IIndividualSet toRetrieveIndividual;
			int unitOfFoodEaten = 0;
			int unitOfFoodOfOne = 0;
			
			int wolfTries =	1 + (iset.size() - huntingWolves.size())
						/ (1 + huntingWolves.size());
			//System.out.println(":"+wolfTries+":");
			int throwNumber = 0;
			for (int j = 0; j < wolfTries; j++) {
				throwNumber = RandomUtils.randomInt(7);
				if( throwNumber < 3) continue; 
				toRetrieveIndividual =
					RWUtils.getRandomIndividuals(weakerHerbivoes, 1);
				//if(RandomUtils.randomInt(2) == 0) continue;
				if (toRetrieveIndividual.size() != 0) {
					individual = (toRetrieveIndividual.toArray())[0];
					unitOfFoodOfOne =
						1
							+ (int) ((((HerbivoreAnimalIndivdual) individual)
								.getNutritionalScore()))
								/ 10;
				//	System.out.print("["+unitOfFoodOfOne+"]" +" "+((HerbivoreAnimalIndivdual)individual).getHuntingScore()+" vs. " + ((AnimalIndividual)individualWolf).getHuntingScore());								
					unitOfFoodEaten += unitOfFoodOfOne;
					weakerHerbivoes.remove(individual);
					iset.remove(individual);
					if (unitOfFoodEaten >= wolfApetite) break;
				} else
					break;
			}
//			while (unitOfFoodEaten < wolfApetite) {
//
//			}
			if (unitOfFoodEaten < wolfApetite) {
				iset.remove(individualWolf);
				//System.out.println("| " + unitOfFoodEaten + " DEAD ");
			} 
			//else System.out.println("| " +unitOfFoodEaten + " OK ");
			 wolfApetite = oldWolfApetite;
		}
		
		++generation; // increment generation counter
		RWUtils.getActualStats(iset);
		return iset;
	}

}
